<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>在线聊天室</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <style>
        html,
        body {
            height: 98%;
        }

        .container-fluid {
            height: calc(100% - 56px);
            /* 56px 是 Bootstrap 导航栏的默认高度 */
            margin-top: 10px;
        }

        #contentFrame {
            width: 100%;
            height: 100%;
            border: none;
        }
    </style>
</head>
<body>
<!-- 头部 -->
<nav class="navbar navbar-expand-lg navbar-dark bg-info">
    <div class="container-fluid">
        <a class="navbar-brand" href="#" th:text="${'在线聊天室 ('+session.user.realName+')'}">在线聊天室</a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
                aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav">
                <li class="nav-item">
                    <a class="nav-link active" href="main.html" th:href="@{content}">首页</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="contact.html" th:href="@{contact}">通讯录</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="profile.html" th:href="@{profile}">个人信息</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="login.html" onclick="logout()">退出登录</a>
                </li>
            </ul>
        </div>
    </div>
</nav>

<!-- 主要内容 -->
<div class="container-fluid  bg-light">
    <iframe id="contentFrame" src="main.html" th:src="@{content}"></iframe>
</div>

<!-- 引入 JavaScript -->
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script>
    // 页面加载完成后执行
    document.addEventListener("DOMContentLoaded", function () {
        // 获取所有的导航链接
        const navLinks = document.querySelectorAll('.nav-link');

        // 为每个导航链接添加点击事件监听器
        navLinks.forEach(link => {
            link.addEventListener('click', function (e) {
                e.preventDefault(); // 阻止默认行为
                const url = this.getAttribute('href');

                navLinks.forEach(link => {
                    link.classList.remove("active")
                })

                this.classList.add("active")

                loadContent(url);
            });
        });

        function loadContent(url) {
            // 加载内容到iframe
            document.getElementById('contentFrame').src = url;
        }
    })

    /**
     * 退出登录
     */
    function logout() {
        if (confirm("确定退出登录吗？")) {
            window.location.href = "/logout";
        }else{
            window.location.href = "/chat";
        }
    }
</script>
</body>
</html>